package com.jsbj.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsbj.base.ResultBean;
import com.jsbj.tools.RequestHeaderUtils;
import com.jsbj.tools.StringUtils;
import com.jsbj.user.entities.SysOrg;
import com.jsbj.user.entities.SysOrgExtend;
import com.jsbj.user.mapper.SysOrgExtendMapper;
import com.jsbj.user.service.ISysOrgExtendService;
import com.jsbj.user.service.ISysOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 标段信息扩展表 服务实现类
 * </p>
 *
 * @author jsbj
 * @since 2022-08-05
 */
@Service
public class SysOrgExtendServiceImpl extends ServiceImpl<SysOrgExtendMapper, SysOrgExtend> implements ISysOrgExtendService {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private ISysOrgService sysOrgService;

    @Override
    public ResultBean<JSONObject> saveOrUpdateData(SysOrgExtend sysOrgExtend) {
        String tenantCode = RequestHeaderUtils.getTenantCode(request);
        String userId = RequestHeaderUtils.getUserId(request);
        // 校验必填
        if (StringUtils.isEmpty(sysOrgExtend.getOrgCode()) || StringUtils.isEmpty(sysOrgExtend.getElectricFence())) {
            return new ResultBean<>().build("必填项不能为空！");
        }
        // 校验编码是否存在
        LambdaQueryWrapper<SysOrg> orgLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orgLambdaQueryWrapper.eq(SysOrg::getDisabled, 0);
        orgLambdaQueryWrapper.eq(SysOrg::getTenantCode, tenantCode);
        orgLambdaQueryWrapper.eq(SysOrg::getOrgCode, sysOrgExtend.getOrgCode());
        orgLambdaQueryWrapper.last(" limit 1");
        SysOrg sysOrg = sysOrgService.getOne(orgLambdaQueryWrapper);
        if (null == sysOrg) {
            return new ResultBean<>().build("组织编码不存在或配置有误，请确认后重试！");
        }
        // 查询是否有记录，有的话更新，没有的话新增
        if (StringUtils.isEmpty(sysOrgExtend.getId())) {
            sysOrgExtend.setTenantCode(tenantCode);
            sysOrgExtend.setCreaterId(userId);
            sysOrgExtend.setUpdateId(userId);
            baseMapper.insert(sysOrgExtend);
        } else {
            SysOrgExtend sysOrgExtend1 = baseMapper.selectById(sysOrgExtend.getId());
            sysOrgExtend1.setElectricFence(sysOrgExtend.getElectricFence());
            sysOrgExtend1.setUpdateId(userId);
            baseMapper.updateById(sysOrgExtend1);
        }

        return new ResultBean<>().success(sysOrgExtend.getId());
    }

    @Override
    public List<SysOrgExtend> getDetail(String orgCode) {
        String tenantCode = RequestHeaderUtils.getTenantCode(request);
        LambdaQueryWrapper<SysOrgExtend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysOrgExtend::getDisabled, 0);
        queryWrapper.eq(SysOrgExtend::getTenantCode, tenantCode);
        queryWrapper.ne(SysOrgExtend::getElectricFence, "[]");
        if (StringUtils.isNotEmpty(orgCode)) {
            queryWrapper.eq(SysOrgExtend::getOrgCode, orgCode);
        } else {
            // 查询有权限的标段
            String userId = RequestHeaderUtils.getUserId(request);
            List<SysOrg> orgs = sysOrgService.getInfoByUserIdAndTenantCode(userId, tenantCode);
            if (!CollectionUtils.isEmpty(orgs)) {
                List<String> orgCodes = new ArrayList<>();
                for (SysOrg sysOrg : orgs) {
                    orgCodes.add(sysOrg.getOrgCode());
                }
                queryWrapper.in(SysOrgExtend::getOrgCode, orgCodes);
            }
        }
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<SysOrgExtend> getDetailApi(String orgCode, String tenantCode) {
        LambdaQueryWrapper<SysOrgExtend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysOrgExtend::getDisabled, 0);
        queryWrapper.eq(SysOrgExtend::getTenantCode, tenantCode);
        if (StringUtils.isNotEmpty(orgCode)) {
            queryWrapper.eq(SysOrgExtend::getOrgCode, orgCode);
        } else {
            return new ArrayList<>();
        }
        return baseMapper.selectList(queryWrapper);
    }
}
